package com.lagou.edu.service.impl;

import com.lagou.edu.dao.AccountDao;
import com.lagou.edu.pojo.Account;
import com.lagou.edu.service.TransferService;



public class TransferServiceImpl implements TransferService {

    //private AccountDao accountDao = new JdbcAccountDaoImpl();
    //private AccountDao accountDao = (AccountDao) BeanFactory.getBean("accountDao");
    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {

/*        try{
            //开启事务（事务和连接对象有关，所以获取当前线程中的连接对象） 业务逻辑方法执行之前 而开启时事务进行手动提交事务，关闭自动提交事务功能，默认开启自动提交事务
            //这里关闭自动提交事务 ，手动控制事务
            TransactionManager.getInstance().beginTransaction();*/
            Account from = accountDao.queryAccountByCardNo(fromCardNo);
            Account to = accountDao.queryAccountByCardNo(toCardNo);

            from.setMoney(from.getMoney()-money);
            to.setMoney(to.getMoney()+money);

            accountDao.updateAccountByCardNo(to);
            //int i = 1/0;
            accountDao.updateAccountByCardNo(from);
/*            //提交事务 获取当前线程连接对象 进行调用commit()手动提交事务（连接工具类设置单例模式 工具类都是同一个连接工具类对象）
            TransactionManager.getInstance().commit();
        }catch (Exception e){
            //出现异常， 执行事务回滚 获取当前线程连接对象
            TransactionManager.getInstance().rollback();
            //注意 这里必须把捕获异常进行抛出 便于上层servlet层捕获
            throw e;
        }*/

    }
}
